﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OneMake.Model
{
    //新老客户销售概况(LZ_D_BDETAIL)
    public class report_customer_sale_stat
    {
        [MapToField(FieldName = "日期")]
        public DateTime stat_datetime { get; set; }

        [MapToField(FieldName = "是否老客户")]
        public string isstickers { get; set; }
	
        [MapToField(FieldName = "购买人数")]
        public int buyer_num { get; set; }

        [MapToField(FieldName = "成交金额")]
        public double amount { get; set; }

        [MapToField(FieldName = "金额占比")]
        public double rate { get; set; }
    }

    partial class ReportHelper
    {
        public List<report_customer_sale_stat> get_report_customer_sale_stat(int shipId, DateTime statTime)
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            List<report_customer_sale_stat> reports = new List<report_customer_sale_stat>();
            string sql = "select shopid,stat_datetime,isstickers,count(member) as buyer_num,sum(alipay_amount) as amount from LZ_D_BDETAIL where shopid={0} and isstickers='{1}' and stat_datetime='{2}'";

            //新顾客
            report_customer_sale_stat report = db.FirstOrDefault<report_customer_sale_stat>(string.Format(sql, shipId, "否", statTime.ToShortDateString()));
            if (report == null)
                return reports;
            
            //老顾客
            report_customer_sale_stat report2 = db.FirstOrDefault<report_customer_sale_stat>(string.Format(sql, shipId, "是", statTime.ToShortDateString()));
            if (report2 == null)
                return reports;
            
            //总计
            report_customer_sale_stat report3 = new report_customer_sale_stat();
            report3.stat_datetime = report.stat_datetime;
            report3.isstickers = "总计";
            report3.buyer_num = report.buyer_num + report2.buyer_num;
            report3.amount = report.amount + report2.amount;
            report3.rate = 1;

            if (report3.amount != 0)
            {
                report.rate = report.amount / report3.amount;
                report2.rate = report2.amount / report3.amount;
            }
            reports.Add(report);
            reports.Add(report2);
            reports.Add(report3);
            return reports;
        }

        public List<report_customer_sale_stat> get_report_customer_sale_stat(int shipId, DateTime beginTime, DateTime endTime)
        {
            System.Diagnostics.Debug.Assert(beginTime <= endTime);

            List<report_customer_sale_stat> reports = new List<report_customer_sale_stat>();
            DateTime tempTime = beginTime;
            do
            {
                reports.AddRange(get_report_customer_sale_stat(shipId, tempTime));
                tempTime = tempTime.AddDays(1);
            }
            while (tempTime <= endTime);

            return reports;
        }
    }
}
